

var ProMan = new Object();

ProMan.start = function() {
	WindLogger.init();
	WindLogger.log("ProMan starting...")
	
	var deskView = new WindView;
	
	deskView.setCssClass("ProManDesk");

	var pageView = new WindView;
	pageView.setCssClass("ProManPage");
	deskView.addView(pageView);

	var headerView = new WindView;
	headerView.setCssClass("ProManPageHeader");
	pageView.addView(headerView);

	var headerRightView = new WindView();
	headerRightView.setCssClass("ProManPageHeaderRight");
	headerView.addView(headerRightView);

	var headerLeftView = new WindView();
	headerLeftView.setCssClass("ProManPageHeaderLeft");
	headerView.addView(headerLeftView);

	var logoView = ProMan.createLogoView();
	headerLeftView.addView(logoView);

	var userInfoView = ProMan.createUserInfoView();
	headerRightView.addView(userInfoView);
	
	var bodyView = new WindView;
	bodyView.setCssClass("ProManPageBody");
	pageView.addView(bodyView);

	var footerView = new WindView;
	footerView.setCssClass("ProManPageFooter");
	pageView.addView(footerView);

	var tabsModel = WindTabs.createModel();
	tabsModel.setId("tabs");
	
	{
		var tabModel = ProMan.createTabModel(tabsModel, "home", "Home", "horizontalTabCode=home");
		if(WindRequestParameters.getRequestParameter("horizontalTabCode")=="home") {
			var tabBodyView = ProManHome.createView();
			bodyView.addView(tabBodyView);
			tabModel.setBody(tabBodyView);
			tabsModel.setActiveTab(tabModel);
		}
	}

	{
		var tabModel = ProMan.createTabModel(tabsModel, "calendar", "Calendar", "horizontalTabCode=calendar");
		if(WindRequestParameters.getRequestParameter("horizontalTabCode")=="calendar") {
			var tabBodyView = ProManCalendar.createView();
			bodyView.addView(tabBodyView);
			tabModel.setBody(tabBodyView);
			tabsModel.setActiveTab(tabModel);
		}
	}
	
	{
		var tabModel = ProMan.createTabModel(tabsModel, "definition", "Definition", "horizontalTabCode=definition");
		if(WindRequestParameters.getRequestParameter("horizontalTabCode")=="definition") {
			var tabBodyView = ProManDefinition.createView();
			bodyView.addView(tabBodyView);
			tabModel.setBody(tabBodyView);
			tabsModel.setActiveTab(tabModel);
		}
	}
	
	{
		var tabModel = ProMan.createTabModel(tabsModel, "planning", "Planning", "horizontalTabCode=planning");
		if(WindRequestParameters.getRequestParameter("horizontalTabCode")=="planning") {
			var tabBodyView = ProManPlanning.createView();
			bodyView.addView(tabBodyView);
			tabModel.setBody(tabBodyView);
			tabsModel.setActiveTab(tabModel);
		}
				
	}
	
	{
		var tabModel = ProMan.createTabModel(tabsModel, "administration", "Administration", "horizontalTabCode=administration");
		if(WindRequestParameters.getRequestParameter("horizontalTabCode")=="administration") {
			var tabBodyView = ProManAdministration.createView();
			bodyView.addView(tabBodyView);
			tabModel.setBody(tabBodyView);
			tabsModel.setActiveTab(tabModel);
		}
				
	}
	
	{
		var cmd = new Object();
		cmd.operation = "datastore.list"
		cmd.type = "horizontalTab"
		cmd.sort = "order";
		
		var result = WindData.execute(cmd)
		
		for(var i=0; i<result.rows.length; i++) {
			var entity = result.rows[i];
			
			var tabModel = ProMan.createTabModel(tabsModel, entity.code, entity.title, entity.url);
			
			if(WindRequestParameters.getRequestParameter("horizontalTabCode")==entity.code) {
				var tabBodyView = ProMan.createTabBodyView(entity.code);
				bodyView.addView(tabBodyView);
				tabModel.setBody(tabBodyView);
				tabsModel.setActiveTab(tabModel);
			}
		}
	}
	
	{	
		var tabModel = ProMan.createTabModel(tabsModel, "framework", "Framework", "horizontalTabCode=framework");
		if(WindRequestParameters.getRequestParameter("horizontalTabCode")=="framework") {
			var tabBodyView = WindFramework.createView();
			bodyView.addView(tabBodyView);
			tabModel.setBody(tabBodyView);
			tabsModel.setActiveTab(tabModel);
		}
	}

	var tabsView = WindTabs.createView(tabsModel);
	tabsView.setId("tabs");
	headerLeftView.addView(tabsView);
	
	WindWindow.addView(deskView);
}

ProMan.createLogoView = function() {
	var view = new WindView();
	view.setId("logo");
	
	var proView = new WindView();
	proView.setId("logoPro");
	proView.setText("Pro");
	view.addView(proView);

	var manView = new WindView();
	manView.setId("logoMan");
	manView.setText("Man");
	view.addView(manView);
	
	return view;
}

ProMan.createUserInfoView = function() {
	var view = new WindView();
	view.setId("userInfo");
	
	var userView = new WindView();
	userView.setId("user");
	userView.setText("ivan.asensio@gmail.com");
	view.addView(userView);
	
	{
		var separatorView = new WindView();
		separatorView.setId("separator");
		separatorView.setText("|");
		view.addView(separatorView);
	}
	
	var profileView = WindLink.createViewFromIdText("profile", "Profile");
	view.addView(profileView);
	
	{
		var separatorView = new WindView();
		separatorView.setId("separator");
		separatorView.setText("|");
		view.addView(separatorView);
	}
	
	var logoutView = WindLink.createViewFromIdText("logout", "Logout");
	view.addView(logoutView);
	
	return view;
}

ProMan.createTabModel = function(tabsModel, id, text, url) {
	var tabModel = tabsModel.createTabModel();
	tabModel.setId(id);
	tabModel.setText(text);
	tabModel.setURL(url);
	
	tabModel.onActivation = function() {
		WindWindow.setTitle("ProMan - " + tabModel.getText());
	}	
	
	return tabModel;
}

ProMan.createTabBodyView = function(horizontalTabCode) {
	var tabBodyView = new WindView();

	var dataView = new WindView();
	dataView.setId("data");

	var layoutView = WindLayout.createView();
	tabBodyView.addView(layoutView);

	var rowView = layoutView.createRowView();

	{
		var actionsModel = new WindPanelModel();
		
		var cmd = new Object();
		cmd.operation = "datastore.list"
		cmd.type = "verticalMenuItem"
		cmd.sort = "order";
		
		var filter = new Object();
		filter.property = "horizontalTabCode";
		filter.operator = "=";
		filter.value = horizontalTabCode;
		
		cmd.filters = new Array();
		cmd.filters[0] = filter;
		
		var result = WindData.execute(cmd)
		
		for(var i=0; i<result.rows.length; i++) {
			var entity = result.rows[i];
			actionsModel.addUrlEntry(entity.title, entity.url);
		}

		var actionsView = WindPanel.createView(actionsModel);
		actionsView.setId("actions");

		var cellView = rowView.createCellView();
		cellView.addView(actionsView);
	}
	
	{
		var cellView = rowView.createCellView();
		cellView.addView(dataView);
		
		var dataTableCode = WindRequestParameters.getRequestParameter("dataTableCode"); 
		if(dataTableCode!=null) {
			var tableView = ProMan.createDataTableView(dataTableCode);
			ProMan.setCurrentView(dataView, tableView);
		}
	}
	
	return tabBodyView;
}

ProMan.setCurrentView = function(dataView, view) {
	if(dataView.currentView!=null) {
		dataView.removeView(dataView.currentView);
	}
	dataView.addView(view);
	dataView.currentView = view;
}

ProMan.createDataTableView = function(dataTableCode) {
	var tableView = null;
	
	var cmd = new Object();
	cmd.operation = "datastore.list"
	cmd.type = "dataTable"
	
	var filter = new Object();
	filter.property = "dataTableCodeCode";
	filter.operator = "=";
	filter.value = dataTableCode;
	
	cmd.filters = new Array();
	cmd.filters[0] = filter;

	var result = WindData.execute(cmd)
	
	if(result.errors!=null && result.errors.length>0) {
		tableView = new WindView();
		for(var i=0; i<result.errors; i++) {
			var error = result.errors[i];
			var labelView = WindLabel.createView();
			labelView.setText(error.text);
			tableView.addView(labelView);
		}
	} else {
		if(result.rows.length>0) {
			var entity = result.rows[i];
			var tableModel = WindTable.createModel();
			tableView = WindTable.createView();
		} else {
			var labelView = WindLabel.createView();
			labelView.setText("Error: Data table with code " + dataTableCode + " no found.");
			tableView = labelView;
		} 
	}

	return tableView;
}
